package com.example.chentian.startprojectcjy313;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.chentian.startprojectcjy313.bean.WeatherInfo;
import com.example.chentian.startprojectcjy313.bean.WeatherResult;
import com.example.chentian.startprojectcjy313.service.WeatherService;
import com.example.chentian.startprojectcjy313.service.WeatherService2;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    final static String TAG = "TAG";

    Button chinaBtn,weatherBtn;
    EditText cityNo;
    TextView cityName,tvCityNo,WenDu,FengLi,FengXiang,ShiDu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        chinaBtn = (Button) findViewById(R.id.button1);
        chinaBtn.setOnClickListener(this);
        weatherBtn = (Button) findViewById(R.id.button2);
        weatherBtn.setOnClickListener(this);

        cityNo = (EditText) findViewById(R.id.et_city_no);
        cityName = (TextView) findViewById(R.id.tv_city);
        tvCityNo = (TextView) findViewById(R.id.tv_city_no);
        FengXiang = (TextView) findViewById(R.id.tv_WD);
        WenDu = (TextView) findViewById(R.id.tv_temp);
        FengLi = (TextView) findViewById(R.id.tv_WS);
        ShiDu = (TextView) findViewById(R.id.tv_SD);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                Toast.makeText(this, "中国天气网", Toast.LENGTH_SHORT).show();
                //这里是点击了中国天气按钮之后的逻辑
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://www.weather.com.cn/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                WeatherService service = retrofit.create(WeatherService.class);
                Call<WeatherResult> call = service.getResult(cityNo.getText().toString());
                call.enqueue(new Callback<WeatherResult>() {
                    @Override
                    public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                        //响应成功了
                        if(response.isSuccessful()&&response.body()!=null){
                            //解析成功
                            Log.d(TAG, "onResponse: 解析成功");
                            WeatherInfo info = response.body().getWeatherinfo();
                            setAllTextView(info);
                        }else{
                            //解析失败
                            Log.d(TAG, "onResponse: 解析失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<WeatherResult> call, Throwable t) {
                        //响应失败了
                        Log.d(TAG, "onFailure: 响应失败");
                    }
                });
                break;

            case R.id.button2:
                Toast.makeText(this, "51查询", Toast.LENGTH_SHORT).show();
                Retrofit retrofit2 = new Retrofit.Builder()
                        .baseUrl("http://weather.51wnl.com/weatherinfo/")
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();

                WeatherService2 service2 = retrofit2.create(WeatherService2.class);
                Call<WeatherResult> call2 = service2.getResult2(cityNo.getText().toString(),1);
                call2.enqueue(new Callback<WeatherResult>() {
                    @Override
                    public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                        //响应成功了
                        if(response.isSuccessful()&&response.body()!=null){
                            //解析成功
                            Log.d(TAG, "onResponse: 解析成功");
                            WeatherInfo info = response.body().getWeatherinfo();
                            setAllTextView(info);
                        }else{
                            //解析失败
                            Log.d(TAG, "onResponse: 解析失败");
                        }
                    }

                    @Override
                    public void onFailure(Call<WeatherResult> call, Throwable t) {
                        //响应失败了
                        Log.d(TAG, "onFailure: 响应失败");
                    }
                });
                break;
        }
    }

    private void setAllTextView(WeatherInfo info){
        cityName.setText(info.getCity());
        tvCityNo.setText(info.getCityid());
        FengLi.setText(info.getWS());
        ShiDu.setText(info.getSD());
        FengXiang.setText(info.getWD());
        WenDu.setText(info.getTemp());
    }
}
